#include "user_fifo.h"
#include <string.h>
#include <stdbool.h>
#include <stdio.h>

uint16_t get_buf_count(array_fifo_t *fifo)
{
    return fifo->length;
}

bool is_full(array_fifo_t *fifo)
{
    return (fifo->length == fifo->max_buf_size);
}

bool is_empty(array_fifo_t *fifo)
{
    return (fifo->length == 0);
}

int push_data(array_fifo_t *fifo, Data_packet_t *p_buf)
{
    if(!is_full(fifo))
    {
        memcpy(&fifo->packet[fifo->write_pos & fifo->max_buf_size], p_buf, sizeof(Data_packet_t));
        fifo->write_pos++;
        fifo->length++;
        return 0;
    }
    else
    {
        return -1;
        //printf("Buf is full\r\n");
    }
}

int pop_data(array_fifo_t *fifo, Data_packet_t *buf)
{
    if(!is_empty(fifo))
    {
        memcpy(&buf, &fifo->packet[fifo->read_pos & fifo->max_buf_size], sizeof(Data_packet_t));
        fifo->read_pos++;
        fifo->length--;
        return 0;
    }
    else
    {
        //printf("Buf is empty\r\n");
        return -1;
    }

}

void array_fifo_init(array_fifo_t *fifo, Data_packet_t *buf, uint16_t buf_size)
{
    fifo->packet = buf;
    fifo->max_buf_size = buf_size;
    fifo->read_pos = 0;
    fifo->write_pos = 0;
    fifo->length = 0;
    
}

